﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WestWorldRemake.Characters;
using WestWorldRemake.Messaging;
using WestWorldRemake.Characters;

namespace WestWorldRemake.States
{
    public class MinersWifeOwned
    {
        
        public class WifesGlobalState : State<MinersWife>
        {
            private WifesGlobalState() { }
            private static WifesGlobalState _Instance;

            public static WifesGlobalState Instance
            {
                get
                {
                    if (_Instance == null)
                        _Instance = new WifesGlobalState();
                    return _Instance;
                }
            }

            public override void Enter(MinersWife wife)
            {
            }
            public override void Execute(MinersWife wife)
            {
                if (!wife.IsCooking())
                {
                    if ((new Random()).NextDouble() < .1
                        && wife.StateMachine.CurrentState != VisitBathroom.Instance)
                    {
                        wife.StateMachine.ChangeState(VisitBathroom.Instance);
                    }
                    else
                    {
                        wife.StateMachine.ChangeState(DoHouseWork.Instance);
                    }
                }
            }
            public override void Exit(MinersWife wife)
            {
            }

            public override Iffy OnMessage(MinersWife character, Telegram t)
            {
                if (t.Message.IsOfType(typeof(Messages.HiHoneyImHome)))
                {
                    Console.WriteLine("\nMessage handled by " + character.name + " at time: " + DateTime.Now.ToShortTimeString());
                    Utilities.WriteText(character, "Hi honey. Let me make you some of mah fine country stew");

                    character.StateMachine.ChangeState(MinersWifeOwned.CookStew.Instance);

                    return Iffy.Yes;
                }
                return Iffy.No;
            }
        }
     
        public class DoHouseWork : State<MinersWife>
        {
            private DoHouseWork() { }
            private static DoHouseWork _Instance;

            public static DoHouseWork Instance
            {
                get
                {
                    if (_Instance == null)
                        _Instance = new DoHouseWork();
                    return _Instance;
                }
            }

            public override void Enter(MinersWife entity)
            {
            }
            public override void Execute(MinersWife entity)
            {
                int val = (int)Math.Floor((double)(new Random()).Next(3));
                switch (val)
                {
                    case 0:
                        Utilities.WriteText(entity, "Moppin' the floor");
                        break;
                    case 1:
                        Utilities.WriteText(entity, "Washin' the dishes");
                        break;
                    case 2:
                        Utilities.WriteText(entity, "Makin' the bed");
                        break;
                }
            }
            public override void Exit(MinersWife entity)
            {
            }

            public override Iffy OnMessage(MinersWife character, Telegram t)
            {
                return Iffy.No;
            }
        }
        
        public class VisitBathroom : State<MinersWife>
        {
            private VisitBathroom() { }
            private static VisitBathroom _Instance;

            public static VisitBathroom Instance
            {
                get
                {
                    if (_Instance == null)
                        _Instance = new VisitBathroom();
                    return _Instance;
                }
            }

            public override void Enter(MinersWife entity)
            {
                Utilities.WriteText(entity, "Walkin' to the can. Need to powda mah pretty li'lle nose");
            }
            public override void Execute(MinersWife entity)
            {
                Utilities.WriteText(entity, "Ahhh! Sweet relief!");
                entity.StateMachine.RevertToPreviousState();
            }
            public override void Exit(MinersWife entity)
            {
                Utilities.WriteText(entity, "Leavin' the John");
            }

            public override Iffy OnMessage(MinersWife character, Telegram t)
            {
                return Iffy.No;
            }
        }
        
        public class CookStew : State<MinersWife>
        {
            private CookStew() { }
            private static CookStew _Instance;

            public static CookStew Instance
            {
                get
                {
                    if (_Instance == null)
                        _Instance = new CookStew();
                    return _Instance;
                }
            }

            public override void Enter(MinersWife entity)
            {
                if (!entity.IsCooking())
                {
                    Utilities.WriteText(entity, "Putting the stew in the oven");
                    MessageDispatcher.Instance.DispatchMessage(1.5, entity, entity, new Messages.StewReady(), MessageDispatcher.NoAdditionalInfo);
                    entity.SetCooking(true);
                }
            }
            public override void Execute(MinersWife entity)
            {
                Utilities.WriteText(entity, "Fussin' over food");
            }
            public override void Exit(MinersWife entity)
            {
                Utilities.WriteText(entity, "Puttin' the stew on the table");
            }

            public override Iffy OnMessage(MinersWife character, Telegram t)
            {
                if (t.Message.IsOfType(typeof(Messages.StewReady)))
                {
                    Console.WriteLine("\nMessage received by " + character.name + " at time: " + DateTime.Now.ToShortTimeString());

                    MessageDispatcher.Instance.DispatchMessage(MessageDispatcher.SendMessageImmediately, character, 
                        EntityManager.Instance.GetEntityByName("Bob"), new Messages.StewReady(), MessageDispatcher.NoAdditionalInfo);

                    character.SetCooking(false);
                    character.StateMachine.ChangeState(DoHouseWork.Instance);

                    return Iffy.Yes;
                }
                return Iffy.No;
            }
        }
            
    }
}
